# 选择结构
# if elif else
"""
if 表达式1 :
    如果表达式1成立则执行
    如果表达式1成立则执行
    如果表达式1成立则执行
elif 表达式2 :
    如果表达式2成立则执行
elif 表达式3 :
    如果表达式3成立则执行
else:
    如果表达式都不成立则执行
"""
# 判断奇偶数
num = 9
if num % 2 == 1:
    print("奇数")
elif num % 2 == 0:
    print("偶数")
else:
    print("不可能")
print("ok")

# 循环结构 while
'''
while 表达式 :
    循环体
    循环体
    循环体
不是循环体
'''
# 1~100的和
i = 1
sum = 0
while i <= 100:
    sum += i
    i += 1
print(sum)  # 不属于while循环体

# for循环主要用于遍历容器
list1 = [1, 2, 3, 4]
tuple1 = (1, 2, 3, 4)
dict1 = {"k1": 'v1', 'k2': 'v2', 'k3': 'v3'}
set1 = {1, 1, 1, 2, 2, 3}

"""
for 变量 in 容器:
    for循环体（依次将容器中的每个元素赋给变量）
"""
# 遍历列表
for i in list1:
    print(i)
# 遍历元组
for i in tuple1:
    print(i)
# 遍历字典
for i in dict1:
    print(i, dict1.get(i))
for i, j in dict1.items():
    print(i, j)

# 遍历集合set
for i in set1:
    print(i)

# range函数
# 指定范围，按照步长生成序列
# 构造一个1~100的list
i = 1
list1 = []
while i <= 100:
    list1.append(i)
    i += 1
print(list1)

list1 = []
for i in range(1, 101, 1):
    if i % 2 == 1:
        list1.append(i)
print(list1)

# 列表推导式
list1 = [i for i in range(1, 101, 1)]
print(list1)

# 构造一个1~100所有的奇数的list
list1 = [i for i in range(1, 101, 1) if i % 2 == 1]
print(list1)

# 9 9 乘法表
# 一行代码
"""
1 * 1 = 1 
2 * 1 = 2 2 * 2 = 4 
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
"""
list11 = []
for i in range(1, 10):
    # list1 = []
    # for j in range(1, i + 1):
    #     list1.append("{} * {} = {}".format(i, j, i * j))
    #     # print("{} * {} = {}".format(i, j, i * j), end=" ")
    list1 = ["{} * {} = {}".format(i, j, i * j) for j in range(1, i + 1)]
    list11.append(list1)
    # print(list1)
print(list11)

list1 = [i for i in range(1, 101, 1)]
for i in range(1, 101, 1):
    j = i
    list1.append(j)
print(list1)

# 列表推导式
list1 = [i for i in range(1, 101, 1)]
print(list1)

list11 = []
for i in range(1, 10):
    list1 = ["{} * {} = {}".format(i, j, i * j) for j in range(1, i + 1)]
    list11.append("\t".join(list1))
print("\n".join(list11))

print("\n".join(["\t".join(["{} * {} = {}".format(i, j, i * j) for j in range(1, i + 1)]) for i in range(1, 10)]))
